﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using HRM.api;
using HRM.Models;
using DTO;
namespace HRM.Controllers
{
    public class NvController : Controller
    {
        //
        // GET: /Nv/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Detail() {
            return View();
        }

        public ActionResult GetNhanVienDetails()
        {
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var nhanviens = new NhanVienAPIController().Get();
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(nhanviens, settings),
                ContentType = "application/json"
            };
            return jsonResult;
        }

        public ActionResult GetInfo(long id)
        {
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var nhanviens = new NhanVienAPIController().Get().Where(p=>p.ID == id);
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(nhanviens, settings),
                ContentType = "application/json"
            };

            return jsonResult;
        }

        public ActionResult GetThanNhan(long id)
        {
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var objs = new ThanNhanController().Get().Where(p => p.NVID == id);
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(objs, settings),
                ContentType = "application/json"
            };

            return jsonResult;
        }

        public ActionResult GetGiaoLy(long id)
        {
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var objs = new QTHocTapController().Get().Where(p => p.NVID == id);
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(objs, settings),
                ContentType = "application/json"
            };

            return jsonResult;
        }

        public ActionResult GetAll()
        {
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var nhanviens = new NhanVienAPIController().Get();
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(nhanviens, settings),
                ContentType = "application/json"
            };

            return jsonResult;
        }

        public ActionResult Get(int pageId, int pageSize)
        {
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var nhanviens = new NhanVienAPIController().Get();
            var itemCount = nhanviens.ToList().Count;
            var items = nhanviens.Skip((pageId-1)*pageSize).Take(pageSize).ToList();
            var result = new PagingInfo<NHANVIEN>()
            {
                ItemCount = itemCount,
                Items = items,
                PageId = pageId,
                PageSize = pageSize
            };
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(result, settings),
                ContentType = "application/json"
            };
            return jsonResult;
        }
    }
}
